/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package manager;

import entity.Contact;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import entity.RegisteredUser;
import java.util.List;

/**
 *
 * @author Stefan
 */
public class ContactManager extends DefaultManager<Contact> {

    public ContactManager() {
        super(Contact.class);
    }

    public boolean contactExists(RegisteredUser user, String email) {
        EntityManager em = getEntityManager();
        String query = "SELECT x FROM Contact x WHERE x.registeredUser = ?1 AND x.deleted = false AND x.email = ?2";
        Query q = em.createQuery(query);
        q.setParameter(1, user);
        q.setParameter(2, email);
        if (q.getResultList().size() > 0) {
            return true;
        } else {
            return false;
        }
    }

    public List<Contact> getContactsForUser(RegisteredUser user) {
        EntityManager em = getEntityManager();
        String query = "SELECT x FROM Contact x WHERE x.registeredUser = ?1 AND x.deleted = 0 ";
        Query q = em.createQuery(query);
        q.setParameter(1, user);
        List<Contact> events = q.getResultList();

        em.close();
        return events;
    }
}
